#!/usr/bin/perl

my $dir = $ARGV[1];
chomp $dir;
#use lib "$dir";
$dir1 = $dir."/bin/parse_cdf";
#use lib "$dir/bin";
use lib "$ARGV[1]/bin";
#use lib "/data1/bsi/bioinf_int/s106381.borawork/data_HTHGU/HTHGU_GeneSumm/bin";
my $fil = $ARGV[0];
chomp $fil;

my $ascii_dir = $ARGV[2];
chomp $ascii_dir;

my $ascii_dir_out = $ascii_dir."out";

print "reading from $fil\n";
use Bio::Affymetrix::WCEL;
#use strict;
my $cel_template=new Bio::Affymetrix::WCEL;
my $cel_ht=new Bio::Affymetrix::WCEL;


open FIN,"<$dir/bin/output_hthgu_hguplus.txt" or die "Can't open $dir/bin/output_hthgu_hguplus.txt : $!";

my $l="";
my %MAP={};
while($l=<FIN>) {
    chomp $l;
    $l =~ s/[\r,\l,\n]+$//g;
    my @line = split(/\t/,$l);
    my $xht=$line[$#line-3];
    my $yht=$line[$#line-2];
    my $xu133=$line[$#line-1];
    my $yu133=$line[$#line];
    my @xy=($xht,$yht);
    $MAP{$xu133 . "-" . $yu133} = \@xy;
}
close FIN;

$cel_template->parse_from_file("$dir/bin/GSM532558_T_Z_HG133.CEL"); # U133A template

#$cel_ht->parse_from_file("/data1/bsi/bioinf_int/s106381.borawork/data_HTHGU/samplefiles_asc/$fil"); # HT source file
$cel_ht->parse_from_file("$dir/temp/$ascii_dir/$fil"); # HT source file

my $im = $cel_template->intensity_map();
my $imht =$cel_ht->intensity_map();

# Print out all of the intensities for each square
my @match;
my @match_whole;
my $num_match =0;
my @mismatch;
my $num_mismatch=0;
for (my $x=0;$x<scalar(@{$cel_template->intensity_map()});$x++) {
        for (my $y=0;$y<scalar(@{$cel_template->intensity_map()->[$x]});$y++) {
	    my $key = $x . "-" . $y;
	    if(exists $MAP{$key}) {
		my $xy_ref = $MAP{$key};
		my $xht = $xy_ref->[0];
		my $yht = $xy_ref->[1];
               # $im->[$x][$y]->[0] =  $imht->[$xht][$yht]->[0] ;
               # $im->[$x][$y]->[1] =  $imht->[$xht][$yht]->[1] ;
               # $im->[$x][$y]->[2] =  $imht->[$xht][$yht]->[2] ;
		$match[$num_match] = $imht->[$xht][$yht]->[0];
		$match_whole[$num_match] = $imht->[$xht][$yht]->[0]."_".$imht->[$xht][$yht]->[1]."_".$imht->[$xht][$yht]->[2] ;
	    	$num_match++;
	    }
	    else
	   {

		$num_mismatch++;

		}
        }
}

@index = sort{ $match[ $a ] <=> $match[ $b ] } 0 .. $#match;
@match = sort {$a <=> $b}@match;
for($i=0;$i<@match_whole;$i++)
{
	$match_whole1[$i] = $match_whole[$index[$i]];

}
$factor = $num_match/$num_mismatch;
for($i=0;$i<$num_mismatch;$i++)
{
	$a = ($factor*($i+1))-1;
       $b = sprintf("%.0f", $a);
 	$mismatch[$i] = $match_whole1[$b];

}

#my $i=0;
#while($i<$num_mismatch) {
#    $factor = $num_mismatch-$i;
#    for(my $b=0;($i<$num_mismatch) && ($b<=$#match_whole1);$b++) {
#	my $frnd = $num_match*rand();
#	if($frnd<=$factor) {
#	    $mismatch[$i]=$match_whole[$b];
#	    $i++;
#	}
#    }
#}

$i_num =0;
#print "second\n";
#print "$num_match\n$num_mismatch\n@match\n@match_whole1\n@mismatch\n";
for (my $x=0;$x<scalar(@{$cel_template->intensity_map()});$x++) {
        for (my $y=0;$y<scalar(@{$cel_template->intensity_map()->[$x]});$y++) {
            my $key = $x . "-" . $y;
            if(exists $MAP{$key}) {
                my $xy_ref = $MAP{$key};
                my $xht = $xy_ref->[0];
                my $yht = $xy_ref->[1];
                $im->[$x][$y]->[0] =  $imht->[$xht][$yht]->[0] ;
                $im->[$x][$y]->[1] =  $imht->[$xht][$yht]->[1] ;
                $im->[$x][$y]->[2] =  $imht->[$xht][$yht]->[2] ;
                }
           else
                {
			@ar = split("_",$mismatch[$i_num]);
			 $im->[$x][$y]->[0] = $ar[0];
			$im->[$x][$y]->[1] = $ar[1];
			$im->[$x][$y]->[2] = $ar[2];
			$i_num++;
               }
        }
}


$cel_template->intensity_map($im);
$cel_template->write_to_file("$dir/temp/$ascii_dir_out/$fil");
